/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ta.web;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.ta.entity.StWorktime;
import com.thinkgem.jeesite.modules.ta.service.StWorktimeService;

/**
 * 设置工作日Controller
 * 
 * @author TTAO
 * @version 2017-06-02
 */
@Controller
@SessionAttributes({ "stWorktime" })
@RequestMapping(value = "${adminPath}/ta/stWorktime")
public class StWorktimeController extends BaseController {

	@Autowired
	private StWorktimeService stWorktimeService;

	@ModelAttribute
	public StWorktime get(@RequestParam(required = false) String id) {
		StWorktime entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = stWorktimeService.get(id);
		}
		if (entity == null) {
			entity = new StWorktime();
		}
		return entity;
	}

	@RequiresPermissions("ta:stWorktime:view")
	@RequestMapping(value = { "index", "" })
	public String index(String year, String month, String date,
			HttpServletRequest request) {
		if (year != null && !year.equals("")) {
			request.setAttribute("year", year);
		}
		if (month != null && !month.equals("")) {
			request.setAttribute("month", month);
		}
		if (date != null && !date.equals("")) {
			request.setAttribute("date", date);
		}
		//DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		List<StWorktime> list = stWorktimeService.findAllListWorkTime();
		request.getSession().setAttribute("list", list);
		return "modules/ta/daily";
	}

	@RequiresPermissions("ta:stWorktime:view")
	@RequestMapping(value = { "upDown", "" })
	public String upDown(String year, String month, String date) {
		String end = "redirect:" + Global.getAdminPath()
				+ "/ta/stWorktime/index?year=" + year + "&month="
				+ (Integer.parseInt(month)) + "&date=" + date;
		return end;
	}

	@RequiresPermissions("ta:stWorktime:view")
	@RequestMapping(value = { "upYear", "" })
	public String upYear(String year, String month, String date) {
		String end = "redirect:" + Global.getAdminPath()
				+ "/ta/stWorktime/index?year=" + year + "&month="
				+ (Integer.parseInt(month)) + "&date=" + date;
		return end;
	}

	@RequiresPermissions("ta:stWorktime:view")
	@RequestMapping(value = { "downYear", "" })
	public String downYear(String year, String month, String date) {
		String end = "redirect:" + Global.getAdminPath()
				+ "/ta/stWorktime/index?year=" + year + "&month="
				+ (Integer.parseInt(month)) + "&date=" + date;
		return end;
	}

	@RequiresPermissions("ta:stWorktime:view")
	@RequestMapping(value = "setDays")
	public String setDays(String date, String six, String zero, String days) {
		DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
		String bDate = date;
		System.out.println(six);
		System.out.println(zero);
		int day = Integer.parseInt(days);
		Date bdate;
		try {
			bdate = format1.parse(bDate);
			Calendar cal = Calendar.getInstance();
			cal.setTime(bdate);
			for (int i = 1; i <= day; i++) {
				cal.add(Calendar.DATE, 1);// num为增加的天数，可以改变的
				bdate = cal.getTime();
				StWorktime stw = stWorktimeService.findDate(bdate);
				if(stw != null){
					stWorktimeService.delete(stw);
				}
				StWorktime stWorktime = stWorktimeService.getWorkTime(format1
						.parse(date));
				if(stWorktime == null){
					return "redirect:" + Global.getAdminPath() + "/ta/stWorktime/index";
				}
				StWorktime stWorktime1 = new StWorktime();
				stWorktime1.setSdate(bdate);
				stWorktime1.setMetime(stWorktime.getMetime());
				stWorktime1.setMstime(stWorktime.getMstime());
				stWorktime1.setNetime(stWorktime.getNetime());
				stWorktime1.setNstime(stWorktime.getNstime());
				stWorktime1.setTimelengh(stWorktime.getTimelengh());
				StWorktime stWorktime2 = new StWorktime();
				stWorktime2.setSdate(bdate);
				stWorktime2.setTimelengh(Long.parseLong("0"));
				// 周六周日
				if (six != null && zero != null && stWorktime != null) {
					if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
							|| cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
						System.out.println(1);
						stWorktimeService.insertWorkTime(stWorktime2);
					} else {
						System.out.println(format1.format(bdate));
						stWorktimeService.insertWorkTime(stWorktime1);
					}
				}
				// 周六
				if (six != null && zero == null && stWorktime != null) {
					if (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
						System.out.println(format1.format(bdate));
						stWorktime1.setSdate(bdate);
						stWorktimeService.insertWorkTime(stWorktime1);
					} else {
						System.out.println(1);
						stWorktimeService.insertWorkTime(stWorktime2);
					}
				}
				// 周日
				if (zero != null && six == null && stWorktime != null) {
					if (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
						System.out.println(format1.format(bdate));
						stWorktimeService.insertWorkTime(stWorktime1);
					} else {
						System.out.println(1);
						stWorktimeService.insertWorkTime(stWorktime2);
					}

				}
				if (six == null && zero == null && stWorktime != null) {
					if (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY
							|| cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
						System.out.println(format1.format(bdate));
						stWorktimeService.insertWorkTime(stWorktime1);
					}
				}
			}
		} catch (ParseException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return "redirect:" + Global.getAdminPath() + "/ta/stWorktime/index";
	}

	@RequiresPermissions("ta:stWorktime:view")
	@RequestMapping(value = { "downUp", "" })
	public String downUp(String year, String month, String date) {
		String end = "redirect:" + Global.getAdminPath()
				+ "/ta/stWorktime/index?year=" + year + "&month="
				+ (Integer.parseInt(month)) + "&date=" + date;
		return end;
	}

	@RequiresPermissions("ta:stWorktime:view")
	@RequestMapping(value = { "list", "" })
	public String list(StWorktime stWorktime, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		Page<StWorktime> page = stWorktimeService.findPage(
				new Page<StWorktime>(request, response), stWorktime);
		model.addAttribute("page", page);
		return "modules/ta/stWorktimeList";
	}

	@RequiresPermissions("ta:stWorktime:view")
	@RequestMapping(value = "form")
	public String form(StWorktime stWorktime, Model model) {
		model.addAttribute("stWorktime", stWorktime);
		return "modules/ta/stWorktimeForm";
	}

	@RequiresPermissions("ta:stWorktime:edit")
	@RequestMapping(value = "save")
	public String save(String mstime, String metime, String nstime,
			String netime, String sdate, String timelengh,
			RedirectAttributes redirectAttributes) throws ParseException {
		StWorktime stWorktime = new StWorktime();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat dfd = new SimpleDateFormat("yyyy-MM-dd");
		// 假期
		StWorktime stWorktime3 = new StWorktime();
		if (!mstime.equals("") && mstime != null) {
			mstime = sdate + " " + mstime;
			stWorktime.setMstime(df.parse(mstime));
		}
		if (metime != null && !metime.equals("")) {
			metime = sdate + " " + metime;
			stWorktime.setMetime(df.parse(metime));
		}
		if (nstime != null && !nstime.equals("")) {
			nstime = sdate + " " + nstime;
			stWorktime.setNstime(df.parse(nstime));
		}
		if (netime != null && !netime.equals("")) {
			netime = sdate + " " + netime;
			stWorktime.setNetime(df.parse(netime));
		}
		if (sdate != null && !sdate.equals("")) {
			stWorktime.setSdate(dfd.parse(sdate));
			StWorktime stWorktime2 = stWorktimeService.getWorkTime(dfd
					.parse(sdate));
			if (stWorktime2 != null) {
				if (timelengh.equals("27000")) {
					stWorktime.setTimelengh(Long.parseLong(timelengh));
					stWorktimeService.updateWorkTime(stWorktime);
					addMessage(redirectAttributes, "修改工作日成功");
				} else {
					stWorktime3.setSdate(dfd.parse(sdate));
					stWorktime3.setTimelengh(Long.parseLong(timelengh));
					stWorktimeService.updateWorkTime(stWorktime3);
					addMessage(redirectAttributes, "修改假期成功");
				}
				return "modules/ta/daily";
			}
		}
		if (timelengh.equals("27000")) {
			stWorktime.setTimelengh(Long.parseLong(timelengh));
			stWorktimeService.insertWorkTime(stWorktime);
			addMessage(redirectAttributes, "保存工作日成功");
		} else {
			stWorktime3.setSdate(dfd.parse(sdate));
			stWorktime3.setTimelengh(Long.parseLong(timelengh));
			stWorktimeService.insertWorkTime(stWorktime3);
			addMessage(redirectAttributes, "保存假期成功");
		}
		return "redirect:" + Global.getAdminPath() + "/ta/stWorktime/index";
	}

	@RequiresPermissions("ta:stWorktime:edit")
	@RequestMapping(value = "getWorktime")
	public String getWorktime(String sdate, String year, String month,
			String date, ModelMap model) throws ParseException {
		SimpleDateFormat dfd = new SimpleDateFormat("yyyy-MM-dd");
		StWorktime stWorktime = stWorktimeService.getWorkTime(dfd.parse(sdate));
		if (stWorktime == null) {
			StWorktime stWorktime2 = new StWorktime();
			stWorktime2.setSdate(dfd.parse(sdate));
			model.put("stWorktime", stWorktime2);
		} else {
			model.put("stWorktime", stWorktime);
		}
		String end = "redirect:" + Global.getAdminPath()
				+ "/ta/stWorktime/index?year=" + year + "&month="
				+ (Integer.parseInt(month)) + "&date=" + date;
		return end;
	}

	@RequiresPermissions("ta:stWorktime:edit")
	@RequestMapping(value = "delete")
	public String delete(StWorktime stWorktime,
			RedirectAttributes redirectAttributes) {
		stWorktimeService.delete(stWorktime);
		addMessage(redirectAttributes, "删除工作日成功");
		return "redirect:" + Global.getAdminPath() + "/ta/stWorktime/index";
	}

}